package mapa
{
	import away3d.cameras.HoverCamera3D;
	import away3d.containers.Scene3D;
	import away3d.containers.View3D;
	import away3d.core.clip.RectangleClipping;
	import away3d.core.utils.Cast;
	import away3d.debug.AwayStats;
	import away3d.materials.BitmapMaterial;
	import away3d.primitives.Plane;
	import away3d.primitives.Sphere;
	
	import com.bit101.components.ProgressBar;
	
	import flash.display.Loader;
	import flash.events.*;
	import flash.net.*;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.UIComponent;
	import mx.managers.SystemManager;
	import mx.messaging.channels.StreamingAMFChannel;
	
	
	//[SWF(frameRate="62", backgroundColor="#FFFF00")]
	public class czysta60 extends UIComponent
	{
		
		private var sm:SystemManager;
		
		private var scene:Scene3D;
		private var camera:HoverCamera3D;
		private var view:View3D;
		private var mat:BitmapMaterial;
		private var primitive:Sphere;
		
		private var bMove:Boolean = false;
		private var lastPanAngle:Number;
		private var lastTiltAngle:Number;
		private var lastMouseX:Number;
		private var lastMouseY:Number;
		private var progressbar:com.bit101.components.ProgressBar;
		public var sTextureURL:String = "";
		
		private var bShiftKeyDown:Boolean = false;
		
		
		
		
		
		
		
		
		public function czysta60(obrazek:int)
		{
			
			sm = new SystemManager();
			
			
			scene = new Scene3D();
			camera = new HoverCamera3D({zoom:10, focus:40});
			camera.minTiltAngle = -20;
			camera.maxTiltAngle = 20;
			camera.panAngle = 90;
			camera.tiltAngle = 0;
			
			camera.zoom = 11;
			camera.focus = 50;
			
			view = new View3D({scene:scene, camera:camera});
			
			
			view.clipping = new RectangleClipping({minX:-300/2,minY:-300/2,maxX:300/2,maxY:300/2});
			
			
			progressbar=new ProgressBar();
			progressbar.x=view.width/2-50;
			progressbar.y=view.height/2-50;
			progressbar.maximum=100;
			
		

			
			
			addChild(view);
			addChild(progressbar);
			
			
			this.primitive = new Sphere({material:"blue#cyan", radius:50000, rotationX:180, segmentsW:30, segmentsH:30});
			
			this.primitive.scale(-1);
			
			
			this.view.scene.addChild(this.primitive);
			
			
			
			
			
		
			loadTexture(obrazek);
			
			
			initListeners();
			
		}
		
		
		private function initListeners():void
		{
			this.addEventListener(Event.ENTER_FRAME, onEnterFrame );
			
		}
		
		
		
		
		
		
		
		
		
		private function onEnterFrame(event:Event):void
		{
			
			this.primitive.rotationY+=0.5;
			
			
			view.render();
		}
			
		public function loadTexture(n:Number):void{
			
			
			
			switch (n){
				case 20:
					sTextureURL = "assets/pan1.jpg"; 
					break;
				
				case 21:
					sTextureURL = "assets/check.png"; 
					break;
				
				case 22:
					sTextureURL = "assets/pan1.jpg"; 
					break;
				
				default:
					trace("ERROR");
					break;
			}
			var pictLdr:Loader = new Loader();
			var pictURLReq:URLRequest = new URLRequest(sTextureURL);
			pictLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, handleFileLoadComplete);
			pictLdr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,handleLoadingProgress);
			pictLdr.load(pictURLReq);
			progressbar.visible = true;
			
		
		}
				
		private function handleLoadingProgress(e:ProgressEvent):void
		{
			var iPercentLoaded:int=e.bytesLoaded/e.bytesTotal*100;
			progressbar.value=iPercentLoaded;

			trace(iPercentLoaded);
		}
			
		private function handleFileLoadComplete(e:Event):void
		{
			this.primitive.material = new BitmapMaterial(Cast.bitmap(e.target.content))
		    progressbar.visible=false;
			
		}
		
		private function clearTexture():void
		{
			this.primitive.material = null;
		}
		
		override protected function updateDisplayList(
			unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
		
			
		}
		
		
}
}